package com.polimi.meteocal.business.entity;

import java.io.Serializable;

import javax.persistence.*;
import java.util.Date;
import java.util.List;


/**
 * The persistent class for the event database table.
 * 
 */
@Entity
@NamedQueries({
	@NamedQuery(name="Event.findAll", query=""
			+ "SELECT e FROM Event e"),
	
	@NamedQuery(name="Event.findById", query=""
			+ "SELECT e FROM Event e "
					+ "WHERE e.id= :eventid"),
					
	@NamedQuery(name="Event.findForUser", query=""
			+ "SELECT e FROM Event e, Ack a "
					+ "WHERE e.id=a.id.eventId AND "
						  + "a.response='ACCEPT' AND "
					      + "a.id.ackSender= :user"),
	
	@NamedQuery(name="Event.findEventsCreated", query=""
			+ "SELECT e FROM User u JOIN u.events e "
					+ "WHERE u.email = :email"),
	@NamedQuery(name="Event.find_by_name_place_datetime", query=""
			+ "SELECT e FROM Event e"
				   + " WHERE e.name = :eventName AND "
					      + "e.place = :eventPlace AND "
				          + "e.datetimebegin = :eventDate")

})
public class Event implements Serializable {
	private static final long serialVersionUID = 1L;
	public static final String FIND_BY_ID = "Event.findById";
	public static final String FIND_ALL = "Event.findAll";
	public static final String FIND_FOR_USER = "Event.findForUser";
	public static final String FIND_EVENTS_CREATED = "Event.findEventsCreated";
	public static final String FIND_EVENT_FOR_NAME_PLACE_DATETIME = "Event.find_by_name_place_datetime";
	@Id
	@Column(name="ID")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;

	@Temporal(TemporalType.TIMESTAMP)
	private Date datetimebegin;

	private String description;

	@Temporal(TemporalType.TIME)
	private Date duration;

	private String name;

	private boolean outdoor;

	private String place;

	private boolean publicvisibility;

	//bi-directional many-to-one association to Invite
	@OneToMany(mappedBy="event", cascade={CascadeType.ALL})
	private List<Invite> invites;

	//bi-directional many-to-many association to User
	@ManyToMany(mappedBy="events")
	private List<User> users;

	//bi-directional many-to-one association to Notification
	@OneToMany(mappedBy="event")
	private List<Notification> notifications;

	//bi-directional one-to-one association to Weather
	@OneToOne(mappedBy="event", cascade={CascadeType.ALL})
	private Weather weather;

	public Event() {
	}

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Date getDatetimebegin() {
		return this.datetimebegin;
	}

	public void setDatetimebegin(Date datetimebegin) {
		this.datetimebegin = datetimebegin;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getDuration() {
		return this.duration;
	}

	public void setDuration(Date duration) {
		this.duration = duration;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean getOutdoor() {
		return this.outdoor;
	}

	public void setOutdoor(boolean outdoor) {
		this.outdoor = outdoor;
	}

	public String getPlace() {
		return this.place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public boolean getPublicvisibility() {
		return this.publicvisibility;
	}

	public void setPublicvisibility(boolean publicvisibility) {
		this.publicvisibility = publicvisibility;
	}

	public List<Invite> getInvites() {
		return this.invites;
	}

	public void setInvites(List<Invite> invites) {
		this.invites = invites;
	}

	public Invite addInvite(Invite invite) {
		getInvites().add(invite);
		invite.setEvent(this);

		return invite;
	}

	public Invite removeInvite(Invite invite) {
		getInvites().remove(invite);
		invite.setEvent(null);

		return invite;
	}

	public List<User> getUsers() {
		return this.users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public List<Notification> getNotifications() {
		return this.notifications;
	}

	public void setNotifications(List<Notification> notifications) {
		this.notifications = notifications;
	}

	public Notification addNotification(Notification notification) {
		getNotifications().add(notification);
		notification.setEvent(this);

		return notification;
	}

	public Notification removeNotification(Notification notification) {
		getNotifications().remove(notification);
		notification.setEvent(null);

		return notification;
	}

	public Weather getWeather() {
		return this.weather;
	}

	public void setWeather(Weather weather) {
		this.weather = weather;
	}

}